let list = [
    {
        id: 1,
        pid: 0,
        name: "总公司",
    },
    {
        id: 2,
        pid: 1,
        name: "北京分公司",
    },
    {
        id: 3,
        pid: 1,
        name: "上海分公司",
    },
    {
        id: 4,
        pid: 2,
        name: "北京子公司",
    },
    {
        id: 5,
        pid: 3,
        name: "上海子公司",
    },
    {
        id: 6,
        pid: 1,
        name: "湖南分公司",
    },
];

const getTree = (arr, rootValue) => {
    let result = []
    arr.forEach(item => {
        if (item.pid === rootValue) {
            result.push(item)
            const child = getTree(arr, item.id)
            child ? item.child = child : ''
        }
    })
    return result
}

console.log(getTree(list, 0))